// ignore_for_file: must_be_immutable

import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;
import 'dart:ui';

import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:intl/intl.dart' as intl;
import 'package:mime/mime.dart' as mime;
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:uuid/uuid.dart';

part 'app/AppMap.dart';
part 'app/HareApp.dart';
part 'app/themes.dart';
part 'basic/Cookies.dart';
part 'basic/DateTimeEx.dart';
part 'basic/EventBus.dart';
part 'basic/basic.dart';
part 'basic/collection_iterable.dart';
part 'basic/collection_list.dart';
part 'basic/collection_sort.dart';
part 'basic/collections.dart';
part 'basic/config_en.dart';
part 'basic/files.dart';
part 'basic/http.dart';
part 'basic/ini.dart';
part 'basic/int_range.dart';
part 'basic/io.dart';
part 'basic/json.dart';
part 'basic/local_store.dart';
part 'basic/logs.dart';
part 'basic/modbus.dart';
part 'basic/msg_center.dart';
part 'basic/numbers.dart';
part 'basic/options.dart';
part 'basic/result.dart';
part 'basic/strings.dart';
part 'basic/text_validator.dart';
part 'basic/values.dart';
part 'binder/Binder.dart';
part 'binder/binder_chip.dart';
part 'binder/binder_radio.dart';
part 'binder/binder_segments.dart';
part 'binder/binder_widgets.dart';
part 'examples/AnimPage.dart';
part 'hare/dash.dart';
part "hare/dash_content.dart";
part "hare/dash_desktop.dart";
part "hare/dash_mobile.dart";
part 'hare/dash_page.dart';
part 'hare/hare_app_bar.dart';
part 'hare/hare_button.dart';
part 'hare/hare_edit.dart';
part 'hare/hare_ext.dart';
part 'hare/hare_page.dart';
part 'hare/hare_text.dart';
part 'hare/hare_widget.dart';
part 'hare/hare_widget_ex.dart';
part 'hare/hare_widgets.dart';
part 'hare/page_attr.dart';
part 'hare/router_data.dart';
part 'hare/ticker_providers.dart';
part 'page/basic_ui.dart';
part 'page/bottom_nav_bar.dart';
part 'page/collection_page.dart';
part 'page/column_page.dart';
part 'page/grid_page.dart';
part 'page/list_page.dart';
part 'page/login_page.dart';
part 'page/login_page_x.dart';
part 'page/page_tab_list.dart';
part 'page/prepare_page.dart';
part 'page/query_panel.dart';
part 'page/refresh_page.dart';
part 'page/search_dialog.dart';
part 'page/tabbar_page.dart';
part 'page/table_page.dart';
part 'page/table_single_page.dart';
part 'query/query_builder.dart';
part 'query/query_dropdown_widget.dart';
part 'query/query_input_widget.dart';
part 'query/query_operator_widget.dart';
part 'query/query_segment.dart';
part 'query/query_single.dart';
part 'widget/actions.dart';
part 'widget/auto_complete.dart';
part 'widget/bread_crumb.dart';
part 'widget/buttons.dart';
part 'widget/chips.dart';
part 'widget/compose.dart';
part 'widget/custom.dart';
part 'widget/data_table.dart';
part 'widget/data_widget.dart';
part 'widget/dialog_builder.dart';
part 'widget/dialogs.dart';
part 'widget/edits.dart';
part 'widget/gird_view.dart';
part 'widget/grid_builder.dart';
part 'widget/grid_delegate.dart';
part 'widget/grid_items.dart';
part 'widget/grid_tile.dart';
part 'widget/int_item_pick.dart';
part 'widget/layout_hare.dart';
part 'widget/list_view.dart';
part 'widget/loading.dart';
part 'widget/overlay.dart';
part 'widget/pagination.dart';
part 'widget/pop_context_menu.dart';
part 'widget/popup.dart';
part 'widget/radios.dart';
part 'widget/scroll.dart';
part 'widget/segments.dart';
part 'widget/slider.dart';
part 'widget/string_widget.dart';
part 'widget/styles.dart';
part 'widget/table.dart';
part 'widget/table_selector.dart';
part 'widget/table_sorter.dart';
part 'widget/text_extension.dart';
part 'widget/toast.dart';
part 'widget/tree_path.dart';
part 'widget/websocket_wrap.dart';
part 'widget/widget_extension.dart';

// part 'widget/radios.dart';
// part 'basic/values.dart';
// part 'basic/files.dart';

final Icon moreArrow = Icons.keyboard_arrow_right.icon();
final IconData MORE_ICON = Icons.adaptive.more_rounded;

extension ColorOp on Color {
  Color withOpacityX(double opacity) {
    assert(opacity >= 0.0 && opacity <= 1.0);
    return this.withAlpha((255.0 * opacity).round());
  }
}

class IndexContext {
  BuildContext context;
  int index;

  IndexContext(this.context, this.index);
}

class ItemIndexContext<T> extends IndexContext {
  T item;

  ItemIndexContext(super.context, super.index, this.item);
}

typedef Edges = EdgeInsets;

EdgeInsets xy(double x, [double y = 0]) {
  return EdgeInsets.symmetric(horizontal: x, vertical: y);
}

EdgeInsets edges({double? left, double? top, double? right, double? bottom, double? hor, double? ver, double? all}) {
  return EdgeInsets.fromLTRB(left ?? hor ?? all ?? 0, top ?? ver ?? all ?? 0, right ?? hor ?? all ?? 0, bottom ?? ver ?? all ?? 0);
  // return EdgeInsets.fromLTRB(left ?? 0, top ?? 0, right ?? 0, bottom ?? 0);
}

EdgeInsets insets({double? left, double? top, double? right, double? bottom, double? hor, double? ver, double? all}) {
  return EdgeInsets.fromLTRB(left ?? hor ?? all ?? 0, top ?? ver ?? all ?? 0, right ?? hor ?? all ?? 0, bottom ?? ver ?? all ?? 0);
  // return EdgeInsets.fromLTRB(left ?? 0, top ?? 0, right ?? 0, bottom ?? 0);
}

extension BuildContextEx on BuildContext {
  void showBottomSheet(List<Widget> children, {CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center}) {
    showModalBottomSheet(
        context: this,
        builder: (c) {
          return ColumnMin(children, crossAxisAlignment: crossAxisAlignment);
        });
  }

  NavigatorState get navigator {
    return Navigator.of(this);
  }

  Future<T?> replacePage<T extends Object>(Widget page) {
    return Navigator.of(this).pushReplacement(MaterialPageRoute(builder: (context) => page));
  }

  Future<T?> pushPage<T extends Object>(Widget page) {
    return Navigator.of(this).push(MaterialPageRoute(builder: (context) => page));
  }

  void popPage<T extends Object>([T? result]) {
    Navigator.of(this).pop(result);
  }

  Future<bool> maybePop<T extends Object>([T? result]) {
    return Navigator.of(this).maybePop(result);
  }

  MediaQueryData get mediaData => MediaQuery.of(this);

  ThemeData get themeData => Theme.of(this);

  bool get isDark {
    return this.themeData.brightness == Brightness.dark;
  }

  bool get largeScreen => mediaData.size.width > 600;

  Color get grayFill {
    return this.isDark ? Colors.grey[700]! : Colors.grey[400]!;
  }
}
